package com.example.filemanager;

import java.io.File;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FileListAdapter extends BaseAdapter{
	Context context;
	List<Map<String, Object>> fileList;
	LayoutInflater inflater;
	
	public FileListAdapter(Context ctx, List<Map<String, Object>> list) {
		this.context = ctx;
		this.fileList = list;
		inflater = LayoutInflater.from(context);
	}
	public void resetData(List<Map<String, Object>> list) {
		this.fileList = list;
	}

	@Override
	public int getCount() {
		return fileList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			holder = new ViewHolder();
			convertView = inflater.inflate( R.layout.file_list_items, null);
			holder.fileName = (TextView) convertView.findViewById(R.id.list_items_itxv_filename);
			holder.img_filetype = (ImageView) convertView.findViewById(R.id.list_items_img_filetype);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		String fileName = (String) fileList.get(position).get("filename");
		String filePath = (String) fileList.get(position).get("filepath");
		holder.fileName.setText(fileName);
		if((new File(filePath)).isDirectory()){
			holder.img_filetype.setImageResource(R.drawable.icon_file);
		}else{
			int type = FileType.checkFileType(filePath);
			holder.img_filetype.setImageResource(type);
		}
		
		
		return convertView;
	}
	
	
	class ViewHolder{
		ImageView img_filetype;
		TextView fileName;
	}

}
